package com.base.taskreports.reports.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.base.taskreports.common.utlis.ExcelUtils;
import com.base.taskreports.common.utlis.FileUtils;
import com.base.taskreports.demoExel.entity.Indicator;
import com.base.taskreports.reports.entiyt.master.ExcelBean;
import com.base.taskreports.reports.service.TIndicatorsService;
import com.base.taskreports.task.service.JfsService;
import com.base.taskreports.task.service.JfuService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/exel")
public class ExelController {
	
	@Autowired
	private TIndicatorsService tIndicatorsService;
	
	
	@Autowired
	private JfuService jfuService;
	@Autowired
	private  JfsService jfsService;
	
	@RequestMapping("/demo1")
	@ResponseBody
	public void demo(String DateTime) throws IOException {
		
	    long startTime = System.currentTimeMillis();
        
        String toDay = new SimpleDateFormat("yyyyMMdd").format(new Date());

        String outPath = "D:\\Users\\admin\\git\\task-reports1\\src\\main\\java\\com\\base\\taskreports\\common\\exel\\报表展示模板.xlsx";
        String newPath = "D:\\Users\\admin\\git\\task-reports1\\src\\main\\java\\com\\base\\taskreports\\common\\exel\\报表展示模板_" + toDay + ".xlsx";

        int stratRow = 3;

        FileUtils.deleteFile(newPath);
        FileUtils.copyFileUsingJava7Files(new File(outPath), new File(newPath));

        ExcelUtils.addHeaderDate(newPath); // 为所有 Sheet 表头加入日期

        
        List<Object> objectList = null;

       // for (Indicator indicator : Indicator.values()) {
            //list = new ArrayList();
            //非货币积分发放金额浮动比率
            List<ExcelBean> getJFS_M001 = tIndicatorsService.getJFS_M001(DateTime);
            this.selectSevenDaysAgo(getJFS_M001,objectList,newPath,stratRow,Indicator.JFS_M001.getIndex(),startTime);
          
            // 非人民币货币积分发放金额浮动比率
            List<ExcelBean>  getJFS_M002 = tIndicatorsService.getJFS_M002( DateTime);
            this.selectSevenDaysAgo(getJFS_M002,objectList,newPath,stratRow,Indicator.JFS_M002.getIndex(),startTime);
            
            // 事后结算积分发放金额浮动比率
            List<ExcelBean>  getJFS_M003 = tIndicatorsService.getJFS_M003( DateTime);
            this.selectSevenDaysAgo(getJFS_M003,objectList,newPath,stratRow,Indicator.JFS_M003.getIndex(),startTime);
           
            // 单用户货币积分余额> N浮动比率
            List<ExcelBean>  getJFS_M004 = tIndicatorsService.getJFS_M004( DateTime);
            this.selectThreeDaysAgo(getJFS_M004,objectList,newPath,stratRow,Indicator.JFS_M004.getIndex(),startTime);
          
            //单用户货币积分余额> 	前50条写入报表
            List<Object> getJFS_M004_50 = jfsService.getJFS_M004_Detail(DateTime);
            this.selectFiftyDaysAgo(getJFS_M004_50,objectList,newPath,10,Indicator.JFS_M004.getIndex(),startTime);
            
            // 单用户非货币积分余额> N浮动比率
            List<ExcelBean>  getJFS_M005 = tIndicatorsService.getJFS_M005( DateTime);
            this.selectThreeDaysAgo(getJFS_M005,objectList,newPath,stratRow,Indicator.JFS_M005.getIndex(),startTime);
           
            // 单用户非货币积分余额> N浮动比率	前50条写入报表
            List<Object> getJFS_M005_50 = jfsService.getJFS_M005_Detail(DateTime);
            this.selectFiftyDaysAgo(getJFS_M005_50,objectList,newPath,10,Indicator.JFS_M005.getIndex(),startTime);
            
            // 临时用户（非注册用户,即不使用userID用户）交易数量浮动比										
            List<ExcelBean>  getJFS_M006 = tIndicatorsService.getJFS_M006( DateTime);
            this.selectSevenDaysAgo(getJFS_M006,objectList,newPath,stratRow,Indicator.JFS_M006.getIndex(),startTime);
           
            // 积分余额为负数量				
            List<ExcelBean>  getJFS_M007 = tIndicatorsService.getJFS_M007( DateTime);
            this.selectThreeDaysAgo(getJFS_M007,objectList,newPath,stratRow,Indicator.JFS_M007.getIndex(),startTime);
          
            // 积分余额为负数量	前50条写入报表
            List<Object> getJFS_M007_50 = jfsService.getJFS_M007_Detail(DateTime);
            this.selectFiftyDaysAgo(getJFS_M007_50,objectList,newPath,10,Indicator.JFS_M007.getIndex(),startTime);
          
            //外部积分（转接/兑换）承兑金额浮动告警
          	List<ExcelBean> getJFU_M001 = tIndicatorsService.getJFU_M001( DateTime);
          	this.selectSevenDaysAgo(getJFU_M001,objectList,newPath,stratRow,Indicator.JFU_M001.getIndex(),startTime);
          	
          	//各积分类别消费中各支付产品类型笔数占比浮动
          	List<ExcelBean> getJFU_M002 = tIndicatorsService.getJFU_M002( DateTime);
          	this.selectSevenDaysAgo(getJFU_M002,objectList,newPath,stratRow,Indicator.JFU_M002.getIndex(),startTime);
          
          	//各积分类别消费中各支付产品类型金额占比浮动
          	List<ExcelBean> getJFU_M003 = tIndicatorsService.getJFU_M003( DateTime);
          	this.selectSevenDaysAgo(getJFU_M003,objectList,newPath,stratRow,Indicator.JFU_M003.getIndex(),startTime);
          
          	//积分撤销/冲正/退货反向交易笔数占比浮动
          	List<ExcelBean> getJFU_M004 = tIndicatorsService.getJFU_M004( DateTime);
          	this.selectSevenDaysAgo(getJFU_M004,objectList,newPath,stratRow,Indicator.JFU_M004.getIndex(),startTime);
          
          	//积分撤销/冲正/退货反向交易金额占比浮动
          	List<ExcelBean> getJFU_M005 = tIndicatorsService.getJFU_M005( DateTime);
          	this.selectSevenDaysAgo(getJFU_M005,objectList,newPath,stratRow,Indicator.JFU_M005.getIndex(),startTime);
          
          	//营销系统系统成功率
          	List<ExcelBean> getJFU_M006 = tIndicatorsService.getJFU_M006( DateTime);
          	this.selectSevenDaysAgo(getJFU_M006,objectList,newPath,stratRow,Indicator.JFU_M006.getIndex(),startTime);
          
          	//积分兑换价值比不为1笔数
          	List<ExcelBean> getJFU_M007 = tIndicatorsService.getJFU_M007( DateTime);
        	this.selectThreeDaysAgo(getJFU_M007,objectList,newPath,stratRow,Indicator.JFU_M007.getIndex(),startTime);
          	
        	//积分兑换价值比不为1笔数 前50
          	List<Object> getJFU_M007_50 = jfuService.getJFU_M007_Detail( DateTime);
        	this.selectFiftyDaysAgo(getJFU_M007_50,objectList,newPath,10,Indicator.JFU_M007.getIndex(),startTime);

        	
        	//已失效积分金额浮动比率
          	List<ExcelBean> getJFU_M008 = tIndicatorsService.getJFU_M008( DateTime);
          	this.selectSevenDaysAgo(getJFU_M008,objectList,newPath,stratRow,Indicator.JFU_M008.getIndex(),startTime);
           
            //System.out.println(ExcelUtils.addExcelSheetContent(newPath, indicator.getIndex(), stratRow, list) ? "成功" : "失败");
       // }

        System.out.println("-------------------------" + (System.currentTimeMillis() - startTime));

        
        
	}
	
	    //积分 --查询前三天的
		public void  selectThreeDaysAgo(List<ExcelBean> excelBeanList,List<Object> objectList, String newPath, int stratRow, int index, long startTime) throws IOException {
			List<List<Object>> list = new ArrayList<List<Object>>(); 
			for (ExcelBean excelBean : excelBeanList) {
            	objectList = new ArrayList<Object>();
                objectList.add(excelBean.getT3());
                objectList.add(excelBean.getT2());
                objectList.add(excelBean.getT1());
                objectList.add(excelBean.getThresholdValue());
                objectList.add(excelBean.getIsOverLimit());
                list.add(objectList);
			}
			 System.out.println(ExcelUtils.addExcelSheetContent(newPath, index, stratRow, list) ? "成功" : "失败");
			 System.out.print(System.currentTimeMillis() - startTime);
			
     }
		
		//积分 --查询前50天的
		public void  selectFiftyDaysAgo(List<Object> excelBeanList,List<Object> objectList, String newPath, int stratRow, int index, long startTime) throws IOException {
			 List<List<Object>> list = new ArrayList<List<Object>>(); 
			 list.add(excelBeanList);
			 System.out.println(ExcelUtils.addExcelSheetContent(newPath, index, stratRow, list) ? "成功" : "失败");
			 System.out.print(System.currentTimeMillis() - startTime);
			
     }
				
				
		//积分 --查询前8天的
		public void   selectSevenDaysAgo(List<ExcelBean> excelBeanList,List<Object> objectList, String newPath, int stratRow, int index, long startTime) throws IOException {
            List<List<Object>> list = new ArrayList<List<Object>>(); 
            for (ExcelBean excelBean : excelBeanList) {
            	objectList = new ArrayList<Object>();
                objectList.add(excelBean.getT8());
                objectList.add(excelBean.getT7());
                objectList.add(excelBean.getT6());
                objectList.add(excelBean.getT5());
                objectList.add(excelBean.getT4());
                objectList.add(excelBean.getT3());
                objectList.add(excelBean.getT2());
                objectList.add(excelBean.getT1());
                objectList.add(excelBean.getFalotRate());
                objectList.add(excelBean.getThresholdValue());
                objectList.add(excelBean.getIsOverLimit());
                list.add(objectList);
			}
			
            System.out.println(ExcelUtils.addExcelSheetContent(newPath, index, stratRow, list) ? "成功" : "失败");
            System.out.print(System.currentTimeMillis() - startTime);
			
	     }
}
